﻿using System;
using System.Security.Principal;

namespace PMngr.Main
{
    /// <summary>
    /// Implements the IPrincipal nterface which defines the basic functionality
    /// of a principal object
    /// </summary>
    public class PMPrincipal : IPrincipal
    {
        /// <summary>
        /// To store the identity associated with this principal
        /// </summary>
        private PMIdentity identityValue;

        /// <summary>
        /// Initializes the class with a new instance of PMIdentity given a 
        /// user name and password
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        public PMPrincipal(string name, string password)
        {
            this.identityValue = new PMIdentity(name, password);
        }

        /// <summary>
        /// Determines whether the current principal belongs to the specified role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool IsInRole(BuiltInRole role)
        {
            foreach (var r in identityValue.Roles)
            {
                if (role == r)
                    return true;
            }


            return false;
        }
        bool IPrincipal.IsInRole(string role)
        {
            BuiltInRole br = (BuiltInRole)Enum.Parse(typeof(BuiltInRole), role);

            return IsInRole(br);
        }

        /// <summary>
        /// Returns the user identity of the current principal
        /// </summary>
        public IIdentity Identity
        {
            get
            {
                return this.identityValue;
            }
        }

    }
}